package cn.bsb.tools;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.jsp.JspException;
import javax.servlet.jsp.tagext.TagSupport;
import java.io.IOException;
import java.util.Enumeration;

/**
 * Created by Qi on 2017/3/5.
 */
public class PageTag extends TagSupport {

    private int pageNumber = 1;
    private int totalCount;
    private int showPageNo = 10;//显示多少页码
    private int pageSize; //每页显示几条记录
    private String url;

    public int getPageNumber() {
        return pageNumber;
    }

    public void setPageNumber(int pageNumber) {
        this.pageNumber = pageNumber;
    }

    public int getTotalCount() {
        return totalCount;
    }

    public void setTotalCount(int totalCount) {
        this.totalCount = totalCount;
    }

    public int getShowPageNo() {
        return showPageNo;
    }

    public void setShowPageNo(int showPageNo) {
        this.showPageNo = showPageNo;
    }

    public int getPageSize() {
        return pageSize;
    }

    public void setPageSize(int pageSize) {
        this.pageSize = pageSize;
    }

    public String getUrl() {
        return url;
    }

    public void setUrl(String url) {
        this.url = url;
    }

    @Override
    public int doStartTag() throws JspException {
//        if (totalCount <= pageSize){
//            return SKIP_BODY;
//        }
        String bar = "";
        try {
            bar += "<ul class=\"pagination\">";
            bar += getNavigatorBar();
            bar += getNavigatorGo();
            bar += "</ul>";
            bar += getJS();
            pageContext.getOut().write(bar);
            return SKIP_BODY;
        }catch (IOException e){
            throw new JspException(e.getMessage());
        }
    }

    @Override
    public int doEndTag() throws JspException {
        return EVAL_PAGE;
    }

    private String getNavigatorBar(){
        int totalPage;
        if (totalCount % pageSize == 0)
            totalPage = totalCount/pageSize;
        else
            totalPage = totalCount/pageSize + 1;
        String bar = "";
        String all = "共"+totalCount+"条数据，共"+totalPage+"页";
        bar += "<li><a>" + all + "</a></li>";
        if (pageNumber == 1){
            bar += "<li class=\"disabled\"><a><i class=\"entypo-left-open-mini\"></i></a></li>";
        } else if (pageNumber > 1){
            String href = url + "?" + getParamUrl() + "pageNumber=1";
            bar += "<li><a href=\"" + href + "\"><i class=\"entypo-left-open-mini\"></i></a></li>";
        }

        if (pageNumber > 3){
            bar += "<li><a>...</a></li>";
        }
        int firstPage = (pageNumber-2)>1?pageNumber-2:1;
        int lastPage = (pageNumber+2)<totalPage?pageNumber+2:totalPage;

        for (int i = firstPage; i <= lastPage; i++){
            if (pageNumber == i){
                bar += "<li class=\"active\"><a>" + i + "</a></li>";
            } else {
                String href = url + "?" + getParamUrl() + "pageNumber=" + i;
                bar += "<li><a href=\"" + href + "\">" + i + "</a></li>";
            }
        }

        if (lastPage < totalPage){
            bar += "<li><a>...</a></li>";
        }

        if (pageNumber < totalPage){
            String href = url + "?" + getParamUrl() + "pageNumber=" + totalPage;
            bar += "<li><a href=\"" + href + "\"><i class=\"entypo-right-open-mini\"></i></a></li>";
        } else if (pageNumber == totalPage){
            bar += "<li class=\"disabled\"><a><i class=\"entypo-right-open-mini\"></i></a></li>";
        }

        return bar;
    }
    
    private String getNavigatorGo(){
    	String bar = "";
    	bar += "<li><a style=\"padding: 0px;\"><input id=\"pageNumIn\" type=\"email\" style=\"border-style:none;padding:0px;border:0px;text-align:center;width:35px;height:29px;\"></a></li>";
    	String href = url + "?" + getParamUrl() + "pageNumber=";
    	bar += "<li><a href=\"javascript:pageToNum('"+ href +"',"+ pageNumber + ")\">Go</a></li>";
    	
    	return bar;
    }
    
    private String getJS(){
    	StringBuffer js = new StringBuffer();
    	js.append("<script type=\"text/javascript\">");
    	js.append("function pageToNum(url, curNum) {");
    	js.append("var pageNum =  $(\"#pageNumIn\").val();");
    	js.append("if (pageNum == \"\") {pageNum = curNum;}");
    	js.append("var href = url + pageNum;");
    	js.append("window.location.href = href;");
    	js.append("}").append("</script>");
    	
    	return js.toString();
    }

    private String getParamUrl(){
        HttpServletRequest request = (HttpServletRequest) pageContext.getRequest();
        Enumeration<String> enumeration = request.getParameterNames();
        String name = null;
        String value = null;
        StringBuffer sb = new StringBuffer();
        while (enumeration.hasMoreElements()){
            name = enumeration.nextElement();
            value = request.getParameter(name);
            if (name.equals("pageNumber"))
                continue;
            sb.append(name).append("=").append(value).append("&");
        }

        return sb.toString();
    }

}
